package org.example.product.controller;

import java.util.List;

import org.example.core.utils.PageUtils;
import org.example.core.utils.R;
import org.example.core.valid.annontation.ListValue;
import org.example.core.valid.group.AddGroup;
import org.example.core.valid.group.UpdateGroup;
import org.example.product.entity.AttributeEntity;
import org.example.product.service.AttributeService;
import org.example.product.vo.AttributeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;




/**
 * @author 小杰_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/pms/backend/attr")
public class AttributeController {
    @Autowired
    private AttributeService attributeService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R<PageUtils> list(@RequestParam(defaultValue = "1") Integer pageNum
                            ,@RequestParam(defaultValue = "10") Integer pageSize
                            ,@RequestParam(required = false) String name
                            ,@RequestParam(required = false) Long categoryId
                            ,@RequestParam(required = false) Integer type){
        PageUtils attrs = attributeService.queryAttrList(pageNum,pageSize,name,categoryId,type);
        return R.ok(attrs);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attributeId}")
    public R<AttributeEntity> info(@PathVariable("attributeId") Long attributeId){
        AttributeEntity entity = attributeService.queryAttributeInfoById(attributeId);
        return R.ok(entity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R<?> save(@Validated(value = {AddGroup.class}) @RequestBody AttributeEntity attribute){
        return attributeService.saveAttributeInfo(attribute)>0? R.ok():R.error();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R<?> update(@Validated(value = {UpdateGroup.class}) @RequestBody AttributeEntity attribute){
        return attributeService.editAttributeInfo(attribute)>0? R.ok():R.error();
    }

    /**
     * 根据分类id获取到属性集合
     */
    @GetMapping("/attrList")
    public R<List<AttributeVo>> attrListByCategoryId(@RequestParam Long categoryId, @RequestParam @ListValue(value = {0,1},message = "参数输入有误") Integer type){
        List<AttributeVo> attributeVos = attributeService.queryAttrsByCategoryId(categoryId,type);
        return R.ok(attributeVos);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R<?> delete(@RequestBody List<Long> ids){
        return attributeService.deletes(ids);
    }

}
